package org.ariadne_eu.hcifetcher.mumeUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.ariadne_eu.hcifetcher.rest.RestUtils;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/entries/enrollment")
public class Enrollment {
	
	/*private Student student;
	private Course course;
	private int score;*/
	
	@Path("/")
	@GET
	@Produces("application/json")
	public String getEnrollmentsByStudentOrCourse(@QueryParam("studentid") String studentid,
												  @QueryParam("courseid") String courseid,
												  @QueryParam("offset") String offset,
												  @QueryParam("size") String size) {
		int intoffset = 0, intsize = 1000;
		try{
			intoffset = Integer.parseInt(offset);
			intsize = Integer.parseInt(size);
		}catch (Exception e) {//can't parse, use default values
		}
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("studentid", studentid);
		filters.put("courseid", courseid);
		return RestUtils.getInstance().JSONToString(RestUtils.getInstance().getEntriesJSON("Enrollment", filters, intoffset, intsize, "courseid", true));
	}
	
	@Path("/studentsofcourse")
	@GET
	@Produces("application/json")
	public String getEnrollmentsByStudentOrCourse(@QueryParam("courseid") String courseid,
												  @QueryParam("offset") String offset,
												  @QueryParam("maxstudentsize") String size) {
		int intoffset = 0, intsize = 1000;
		try{
			intoffset = Integer.parseInt(offset);
			intsize = Integer.parseInt(size);
		}catch (Exception e) {//can't parse, use default values
		}
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("courseid", courseid);
		Vector<JSONObject> enrollments = RestUtils.getInstance().getEntriesJSON("Enrollment", filters,intoffset, intsize, "score", false);
		Vector<JSONObject> students = new Vector<JSONObject>();
		for(JSONObject enrollment : enrollments){
			JSONObject student = null;
			try {
				student = RestUtils.getInstance().getEntityById(enrollment.getString("studentid"));
				student.put("coursescore", enrollment.get("score"));
			} catch (JSONException e) {
			}
			students.add(student);
		}
		return RestUtils.getInstance().JSONToString(students);
	}
	
	@Path("/coursesofstudent")
	@GET
	@Produces("application/json")
	public String getCoursesOftudent(@QueryParam("studentid") String studentid,
												  @QueryParam("offset") String offset,
												  @QueryParam("maxcoursessize") String size) {
		int intoffset = 0, intsize = 1000;
		try{
			intoffset = Integer.parseInt(offset);
			intsize = Integer.parseInt(size);
		}catch (Exception e) {//can't parse, use default values
		}
		return RestUtils.getInstance().JSONToString(getCoursesOfStudent(studentid, intoffset, intsize));
	}

	public Vector<JSONObject> getCoursesOfStudent(String studentid, int intoffset,
			int intsize) {
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("studentid", studentid);
		Vector<JSONObject> enrollments = RestUtils.getInstance().getEntriesJSON("Enrollment", filters,intoffset, intsize, "courseid", false);
		Vector<JSONObject> courses = new Vector<JSONObject>();
		for(JSONObject enrollment : enrollments){
			JSONObject course = null;
			try {
				course = RestUtils.getInstance().getEntityById(enrollment.getString("courseid"));
			} catch (JSONException e) {
			}
			courses.add(course);
		}
		return courses;
	}

}
